package com.example.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 模拟裁判发令，参赛选手赛跑的过程
 */
public class CountDownLatchTest2 {
    public static void main(String[] args) {
        test1(5);
    }

    public static void test1(int length){
        ExecutorService excutor = Executors.newFixedThreadPool(length);
        CountDownLatch gunner = new CountDownLatch(1);
        CountDownLatch runner = new CountDownLatch(length);
        for(int i=0; i<length; i++) {
            Thread thread = new Thread(() -> {
                try {
                    System.out.println("选手： " + Thread.currentThread().getName() + "准备就绪，等待裁判开枪");
                    gunner.await();
                    System.out.println("选手： " + Thread.currentThread().getName() + "向前冲刺！");
                    Thread.sleep((long)(Math.random() * 10000));
                    System.out.println("选手： " + Thread.currentThread().getName() + "完成比赛！");
                    runner.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            excutor.execute(thread);
        }

        try {
            Thread.sleep(5000);
            System.out.println("裁判： " + Thread.currentThread().getName() + "即将开枪");
            gunner.countDown();
            System.out.println("裁判： " + Thread.currentThread().getName() + "开枪，比赛开始");
            runner.await();
            System.out.println("所有选手到达终点");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        excutor.shutdown();
    }

    public static void test2(int length){
        ExecutorService excutor = Executors.newCachedThreadPool();
        final CountDownLatch gunner = new CountDownLatch(1);
        final CountDownLatch runner = new CountDownLatch(length);
        for (int i = 0; i < length; i++) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("选手： " + Thread.currentThread().getName() + "准备就绪，等待裁判开枪");
                        gunner.await();
                        System.out.println("选手： " + Thread.currentThread().getName() + "向前冲刺！");
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println("选手： " + Thread.currentThread().getName() + "完成比赛！");
                        runner.countDown();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            excutor.execute(runnable);
        }
        try {
            Thread.sleep((long) (Math.random() * 10000));
            System.out.println("裁判： " + Thread.currentThread().getName() + "即将开枪");
            gunner.countDown();
            System.out.println("裁判： " + Thread.currentThread().getName() + "开枪，比赛开始");
            runner.await();
            System.out.println("所有选手到达终点");
            System.out.println("裁判"+Thread.currentThread().getName()+"汇总成绩排名");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        excutor.shutdown();
    }
}
